package com.ymt.bpm.web.rest.platform;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.service.platform.websocket.WebSocketService;
import com.ymt.bpm.service.platform.websocket.WsMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/ws")
public class WebSocketResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WebSocketService ws;

    @GetMapping("/testws")
    public String testWs() throws Exception {
        for(int i=0;i<3;i++) {
            Thread.sleep(1000);
            ws.sendMessage(null, "task", "completeTask","message "+i);
            log.info("----------------------send "+i);
        }
        return "ok";
    }

    @MessageMapping("/ws/endpoint/backend")
    public JSONObject sendToBackend(WsMessage message) {
        if (log.isDebugEnabled()) {
            log.debug("sendToBackend received message="+ message);
        }
        JSONObject rtn = new JSONObject();
        return rtn;
    }

}
